// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import UIScrollControl from "../plugins/UIScrollControl";
import { wxAvatar } from "../Utils";
import ResourceManager from "../manager/ResourceManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RankLayer extends BaseLayer {

    panel: cc.Node = null
    btnClose: cc.Node = null
    scrollNode: cc.Node = null
    scrollItem: cc.Node = null
    nodeMy: cc.Node = null
    contentNode: cc.Node = null
    // 记录加载过的，避免多次加载
    wxAvatarUrl: string[] = []

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('btn_close', this.panel)
        this.scrollNode = cc.find('scroll', this.panel)
        this.scrollItem = PoolManager.instance.getNode('RankItem')
        this.btnClose.on('click', this.onCloseClick, this)
        this.nodeMy = cc.find('my', this.panel)
        this.contentNode = cc.find('scrollLayout/view/content', this.panel)
    }

    onDestroy() {
        this.btnClose.off('click', this.onCloseClick, this)
    }

    onEnable() {
        this.fadeIn(this.panel)
        this.rendor()
    }

    async rendor() {
        if (!DataManager.instance.user) return
        const obj = await DataManager.instance.wsClient.callApi('RankGet', { openid: DataManager.instance.user.openid })
        if (!obj.isSucc) return
        const res = obj.res
        const top = res['top']
        const my = res['my']
        let isScrollToTop = false
        this.scrollNode.getComponent(UIScrollControl).init(this.scrollItem, top.length, cc.size(500, 110), 0, (node: cc.Node, index: number) => {
            if (!isScrollToTop) {
                isScrollToTop = true
                this.scrollNode.getComponent(cc.ScrollView).scrollToTop()
            }
            const item = top[index]
            const level = item.rank
            const rank = node.getChildByName('rank')
            const main = node.getChildByName('main')
            const avatar = node.getChildByName('avatar')
            rank.getChildByName('icon1').active = false
            rank.getChildByName('icon2').active = false
            rank.getChildByName('icon3').active = false
            rank.getChildByName('label').getComponent(cc.Label).string = `${level}`
            if (level <= 3) {
                rank.getChildByName(`icon${level}`).active = true
            }
            main.getChildByName('name').getComponent(cc.Label).string = `${item['nickname']}`
            const winLabel = main.getChildByName('score').getChildByName('win')
            const killLabel = main.getChildByName('score').getChildByName('kill')
            winLabel.getChildByName('num').getComponent(cc.Label).string = `${item['win']}`
            killLabel.getChildByName('num').getComponent(cc.Label).string = `${item['kill']}`
            if (item['avatar']) {
                if (this.wxAvatarUrl.indexOf(item['avatar']) < 0) {
                    this.wxAvatarUrl.push(item['avatar'])
                    wxAvatar(item['avatar']).then((texture: any) => {
                        avatar.getChildByName('icon').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture)
                    })
                }
            } else {
                avatar.getChildByName('icon').getComponent(cc.Sprite).spriteFrame = ResourceManager.instance.getSprite('222')
            }
        }, (scroll: cc.ScrollView) => {
            scroll.scrollToTop()
        })
        // 个人排行
        if (my.length) {
            this.nodeMy.getComponent(cc.Label).string = `你的排名：第${my[0]['rank']}名`
        } else {
            this.nodeMy.getComponent(cc.Label).string = ''
        }
    }

    onDisable() { }

    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.RANK, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }
}
